<template>
    <div class="white-box">
        <div class="task-one">
            <div class="task-one__item">
                <span class="task-one__label">文章标题：</span>
                <el-input placeholder="输入文章标题" v-model.trim="search_text" size="default" clearable></el-input>
            </div>
            <div class="task-one__btn clearfix">
                <el-button type="primary" :loading="search_btn" :icon="Search" @click="handleSearch">搜索</el-button>
            </div>
        </div>

        <div class="table-mb">
            <el-button type="primary" :icon="CirclePlus" @click="handleAdd">添加</el-button>
        </div>

        <el-table class="table-mb" v-loading="loading" :data="arr_data" :header-cell-style="{ background: '#F5F7FA' }"
            element-loading-background="rgba(255, 255, 255, 0.6)" @selection-change="handleAllSelected">
            <!-- <el-table-column type="selection" width="55" /> -->
            <el-table-column prop="is_type" label="文章分类ID" width="130"> </el-table-column>
            <el-table-column prop="title" label="标题" min-width="180"> </el-table-column>
            <el-table-column prop="is_type" label="分类">
				<template #default="{ row }">
					<div>{{typeLabel[row.is_type]}}</div>
				</template>
			</el-table-column>
            
            <el-table-column prop="created_at" label="发布时间" width="160"> </el-table-column>
            <el-table-column label="操作" width="160" fixed="right">
                <template #default="{ row }">
                    <el-button class="theme-btn" link type="primary" @click="handleEdit(row.id)">编辑
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        

        <go-back />
    </div>
</template>

<script setup>
import { getCurrentInstance, ref, onMounted, watch } from "vue"
import OneLoad from "@mixins/one-load"
import { useRouter } from "vue-router"
import { Delete, CirclePlus, Search } from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"

const router = useRouter()
const { proxy } = getCurrentInstance()

onMounted(() => {
    getList();
})

const arr_data=ref([])
const typeLabel=ref({
	1:"用户协议",
	2:"购物协议",
	3:"骑手协议",
	4:"隐私协议",
	6:"商家协议",
})
function getList() {
    
	proxy.$axios
	    .get("admin/agreement/list")
	    .then((res) => {
	        
	        if (res.data.code == 0) {
	           arr_data.value=res.data.data
	        } else {
	            proxy.$message({ message: res.data.msg, type: "error" })
	        }
	    })
	    .catch((err) => {
	        
	    })
}

const handleSearch = () => {
    search_btn.value = true
    getList();
}

// 删除单个
const handleDelete = (row) => {
    postDel(row.article_id)
}
// 删除多个
const ids = ref([])
const handleAllSelected = (val) => {
    ids.value = val
}
const allDel = () => {
    if (ids.value.length == 0) {
        proxy.$message({message:"请选择需要删除的数据",type:"warning"})
        return
    }
    let arrs = []
    for (let i = 0; i < ids.value.length; i++) {
        arrs.push(ids.value[i].article_id)
    }
    postDel(arrs)
}
// 删除接口
function postDel(params) {
    proxy.$axios
        .post("article/delete", {
            article_id: params,
        })
        .then((res) => {
            if (res.data.code == 0) {
                getList();
            } else {
                proxy.$message({message:res.data.msg,type:"error"})
            }
        })
        .catch((err) => { })
}

// 添加
const handleAdd = () => {
    router.push('/News/NewsAdd')
}
// 编辑
const handleEdit = (eid) => {
    router.push({
        path: '/News/NewsEdit',
        query: {
            eid
        }
    })
}

</script>
<script>
export default {
    name: "NewsList",
}
</script>

<style lang="less" scoped>
.status-text {
    color: @themes;
}
</style>